package pku.tcs.wsc.parser;

import java.util.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import pku.tcs.wsc.service.*;

/**
 * 
 * @author Cai Shaowei
 *
 */
public class WSDLParser {
	
	private String wsdlFilePath;
	private ServicePool     pool;
	
	public	WSDLParser(String filepath,ServicePool pool){
		this.pool = pool;
		wsdlFilePath = filepath;
	}
	
	public void parse()
	{
		SAXBuilder sb = new SAXBuilder();
		Document   doc = null;
		
		try{ 
			doc = sb.build(new FileInputStream(wsdlFilePath));}
		catch( IOException e ){   
            e.printStackTrace();   
        }   catch ( JDOMException e){
        	e.printStackTrace();   
        }
  
        //root
        Element Root = doc.getRootElement();
        //sub elements
        List rootChildren = Root.getChildren();
        //get the last element "mece:semMessageExt"
        Element meceRoot = (Element) rootChildren.get(rootChildren.size()-1);
        //all the messages organized by service id
        List msgList = meceRoot.getChildren();
        
        String tmp;

        for(int i=0;i<msgList.size(); i+=2) {
    	   
    	   Service s = new Service();
    	   
    	   //The Request Message
    	   Element msg = (Element) msgList.get(i);
    	   
    	   //get service id
    	   tmp = msg.getAttributeValue("id");   
    	   tmp = tmp.replace("serv","");
    	   tmp = tmp.replace("RequestMessage","");
    	   s.id = tmp;
    	  
    	   
    	   //get service input parameters
    	   List inputList = msg.getChildren();
    	   for(int j=0;j<inputList.size();j++)
    	   {
    		   Element parameter = (Element) inputList.get(j);
    		   List instList = parameter.getChildren();
    		   Element instPara = (Element) instList.get(0);
    		   
    		   tmp = instPara.getText();
    		   tmp = tmp.substring(tmp.indexOf("inst"));
    		   
    		   s.inputInstances.add(tmp);  
    	   }
    	   
    	   //The Response Message
    	   msg = (Element) msgList.get(i+1);
    	   //get output parameters
    	   List OutputList = msg.getChildren();
    	   for(int j=0;j<OutputList.size();j++)
    	   {
    		   Element parameter = (Element) OutputList.get(j);
    		   List InstList = parameter.getChildren();
    		   Element InstPara = (Element) InstList.get(0);
    		   
    		   tmp = InstPara.getText();
    		   tmp = tmp.substring(tmp.indexOf("inst"));
    		   
    		   s.outputInstances.add(tmp); 
    	   }
    	   //add the service to pool
			try {
				pool.addService(s);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }    
	}
	

	
	public Service parseChallengeFile(String challengFileName)throws IOException
	{
		Vector<Service> VecService = new Vector<Service> ();
		
		SAXBuilder sb=new SAXBuilder();
		Document Doc = null;
		
		List RootChildren = null;
		List MsgList = null;
		
		try{ Doc = sb.build(new FileInputStream(challengFileName));} 
		catch( IOException e )
		{   
			e.printStackTrace();   
		}   
		catch ( JDOMException e)
		{
			e.printStackTrace();   
		}
    
		Element Root = Doc.getRootElement();
        
		RootChildren = Root.getChildren();
        
		Element MeceRoot = (Element) RootChildren.get(RootChildren.size()-1);//get the last element "mece:semMessageExt"
        
		MsgList = MeceRoot.getChildren();
        
		String tmp;

		for(int i=0;i<MsgList.size(); i+=2) 
		{
    	   
			Service s = new Service();
    	   
			//The Request Message
			Element Msg = (Element) MsgList.get(i);
    	   
			//get service id
			tmp = Msg.getAttributeValue("id");   
			tmp = tmp.replace("serv","");
			tmp = tmp.replace("RequestMessage","");
			s.id = tmp;
    	   
			//get service input parameters
			List InputList = Msg.getChildren();
			for(int j=0;j<InputList.size();j++)
			{
				Element parameter = (Element) InputList.get(j);
				List InstList = parameter.getChildren();
				Element InstPara = (Element) InstList.get(0);
    		   
				tmp = InstPara.getText();
				tmp = tmp.substring(tmp.indexOf("inst"));
    		   
				s.inputInstances.add(tmp); 
			}
    	   
			//The Response Message
			Msg = (Element) MsgList.get(i+1);
			//get output parameters
			List OutputList = Msg.getChildren();
			for(int j=0;j<OutputList.size();j++)
			{
				Element parameter = (Element) OutputList.get(j);
				List InstList = parameter.getChildren();
				Element InstPara = (Element) InstList.get(0);
    		   
				tmp = InstPara.getText();
				tmp = tmp.substring(tmp.indexOf("inst"));
    		   
				s.outputInstances.add(tmp); 
			}
    	   
			//add the service to vector
			VecService.add(s);
    	   
		}
		
		return VecService.elementAt(0);
	}
	
	
	public static void main(String[] args){
		ServicePool pool = new ServicePool();
		WSDLParser parser = new WSDLParser("./data/Services.wsdl",pool);
		try {
			parser.parse();
		} catch (Exception e) {
		}
	}	
}



